﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SP.OpenSource.CDC.Monitor
{
    public partial class MonitorCDCMain : Form
    {
        private String sConnCDC = String.Empty;
        private String sConfiguration = String.Empty;
        private String m_sConfigurationID = String.Empty;
        private String m_sEncryptType = String.Empty;

        private TabPage tpCDCManagement = new TabPage("CDC Management (Enable/Disable)");
        private TabPage tpCDCCapturedData = new TabPage("Captured Data View");
        private TabPage tpCDCLogs = new TabPage("CDC Logs");
        private TabPage tpCDCConfigMonitor = new TabPage("Configure Monitoring");
        public bool m_bLogout = false;

        String m_sConnectedDBName = String.Empty;
        String m_sConnectedServerName = String.Empty;
        String m_sLicenseType = String.Empty;
        String m_sErrorMessage = String.Empty;
        String m_sFormTitle = "Monitor CDC - Main Screen";
        String m_sGenericErrorMessage = "Exception raised while processing request." + Environment.NewLine + "Error Message: ";

        public MonitorCDCMain()
        {
            InitializeComponent();
        }

        public MonitorCDCMain(String sConnStr,  String sConfig, String sConfigID, String sEncryptType)
        {
            InitializeComponent();
            try
            {                
                sConnCDC = sConnStr;
                
                sConfiguration = sConfig;
                m_sConfigurationID = sConfigID;
                
                m_sEncryptType = sEncryptType;
                                
                tspConnectedMeta.Text = "You are connected to... " ;
                tspConfig.Text = sConfiguration;

                foreach(String sT in sConnCDC.Split(';'))
                {
                    if(sT.Contains("Data Source"))
                    {
                        int iCnt = sT.IndexOf("=");
                        iCnt += 1;
                        if(iCnt <= sT.Length)
                            m_sConnectedServerName = sT.Substring(iCnt);
                    }
                    if (sT.Contains("Initial Catalog"))
                    {
                        if (sT.IndexOf("=") + 1 <= sT.Length)
                            m_sConnectedDBName = sT.Substring(sT.IndexOf("=") + 1);
                    }
                }
                if (m_sConnectedServerName.Length > 0)
                {
                    tspLblServerName.Text = m_sConnectedServerName;                    
                }

                if(m_sConnectedDBName.Length > 0)
                    tspSelectedDB.Text = m_sConnectedDBName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(m_sGenericErrorMessage + ex.Message, m_sFormTitle + " - error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        
        private void tspLogoff_Click(object sender, EventArgs e)        
        {
            m_bLogout = true;
            this.Close();                    
        }
                
        private void CDCControllerMain_Load(object sender, EventArgs e)
        {
            try
            {
                tabMain.TabPages.Add(tpCDCManagement);
                tpCDCManagement.Controls.Add(new CDCManagement(sConnCDC, m_sConnectedDBName, m_sConnectedServerName, m_sConfigurationID));

                BLL.CDCManagement m_oCDCManagement = new BLL.CDCManagement();
                if (true == m_oCDCManagement.IsCDCEnabledDB(sConnCDC, m_sConnectedDBName, ref m_sErrorMessage))
                {
                    tabMain.TabPages.Add(tpCDCCapturedData);
                    tpCDCCapturedData.Controls.Add(new CDCcapturedData(sConnCDC, m_sConnectedDBName, m_sConnectedServerName, m_sConfigurationID, m_sEncryptType));

                    tabMain.TabPages.Add(tpCDCLogs);
                    tpCDCLogs.Controls.Add(new CDCLogs(sConnCDC, m_sConnectedDBName, m_sConnectedServerName, m_sConfigurationID));


                    tabMain.TabPages.Add(tpCDCConfigMonitor);
                    tpCDCConfigMonitor.Controls.Add(new MonitorConfig(sConnCDC, m_sConnectedDBName, m_sConnectedServerName, m_sConfigurationID, m_sEncryptType));

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(m_sGenericErrorMessage + ex.Message, m_sFormTitle + " - error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tabMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                TabControl tpTemp = (TabControl)sender;
                if (null != tpTemp && tpTemp.SelectedTab.Text == "CDC Management (Enable/Disable)")
                {
                    foreach (Control ctlT in tpCDCManagement.Controls)
                    {
                        String sT = ctlT.ToString();
                        if (ctlT.ToString() == "SP.OpenSource.CDC.Monitor.CDCManagement")
                        {
                            SP.OpenSource.CDC.Monitor.CDCManagement oCDCManagement = (SP.OpenSource.CDC.Monitor.CDCManagement)ctlT;
                            oCDCManagement.LoadCDCManagementInfo();                           
                        }

                    }
                }
                else if (null != tpTemp && tpTemp.SelectedTab.Text == "Captured Data View")
                {
                    foreach (Control ctlT in tpCDCCapturedData.Controls)
                    {
                        String sT = ctlT.ToString();
                        if (ctlT.ToString() == "SP.OpenSource.CDC.Monitor.CDCcapturedData")
                        {
                            SP.OpenSource.CDC.Monitor.CDCcapturedData oCDCcapturedData = (SP.OpenSource.CDC.Monitor.CDCcapturedData)ctlT;
                            oCDCcapturedData.LoadCDCCaptureInfo();
                        }

                    }
                }
                else if (null != tpTemp && tpTemp.SelectedTab.Text == "Configure Monitoring")
                {
                    foreach (Control ctlT in tpCDCConfigMonitor.Controls)
                    {
                        String sT = ctlT.ToString();
                        if (ctlT.ToString() == "SP.OpenSource.CDC.Monitor.MonitorConfig")
                        {
                            SP.OpenSource.CDC.Monitor.MonitorConfig oMonitor = (SP.OpenSource.CDC.Monitor.MonitorConfig)ctlT;
                            oMonitor.LoadMonitorConfigForm();
                        }

                    }
                }
                else if (null != tpTemp && tpTemp.SelectedTab.Text == "CDC Logs")
                {
                    foreach (Control ctlT in tpCDCLogs.Controls)
                    {
                        String sT = ctlT.ToString();
                        if (ctlT.ToString() == "SP.OpenSource.CDC.Monitor.CDCLogs")
                        {
                            SP.OpenSource.CDC.Monitor.CDCLogs oCDCLogs = (SP.OpenSource.CDC.Monitor.CDCLogs)ctlT;
                            oCDCLogs.LoadCDCLogsForm();
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(m_sGenericErrorMessage + ex.Message, m_sFormTitle + " - error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            String sExeFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            System.Diagnostics.Process.Start(sExeFilePath + "\\Help\\HelpMonitorCDC.chm");
        }
    }
}
